﻿using PLCWpfTest.Common.Extensions;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace PLCWpfTest.Controls
{
    /// <summary>
    /// WindowTop.xaml 的交互逻辑
    /// </summary>
    public partial class WindowTop : UserControl
    {
        private static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(WindowTop), new PropertyMetadata(string.Empty, null, TitleCoerceValueCallback));

        [Bindable(true), Category("自定义属性"), Description("窗体顶部标题")]
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        [Category("自定义属性"), Description("是否可以最小化")]
        public bool Minimize { get; set; } = false;

        [Category("自定义属性"), Description("是否可以最大化")]
        public bool Maximize { get; set; } = false;

        public WindowTop()
        {
            InitializeComponent();
            MinBtn.Visibility = Minimize ? Visibility.Visible : Visibility.Collapsed;
            MaxBtn.Visibility = Maximize ? Visibility.Visible : Visibility.Collapsed;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            Window.GetWindow(this).OnMouseDownMethod(e);
        }

        private void MinBtn_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(this).WindowState = WindowState.Minimized;
        }

        private void MaxBtn_Click(object sender, RoutedEventArgs e)
        {
            Window window = Window.GetWindow(this);
            if (window.WindowState == WindowState.Maximized)
            {
                window.WindowState = WindowState.Normal;
                MaxBtn.Content = "\ue7b2";
            }
            else
            {
                window.WindowState = WindowState.Maximized;
                MaxBtn.Content = "\ue7b1";
            }
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(this).Close();
        }

        private static object TitleCoerceValueCallback(DependencyObject d, object baseValue)
        {
            if (baseValue == null)
            {
                return baseValue;
            }
            string title = baseValue.ToString();
            ((WindowTop)d).TitleLabel.Content = title;
            return title;
        }
    }
}
